package com.jimmy.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

import javax.sql.DataSource;

import com.jimmy.dao.wt_billpay;
import com.jimmy.pool.myOracle;

public class billpayBean {
	DataSource ds;
	Connection con;
	PreparedStatement ps;
	
	ResultSet rset = null;
	
	public Collection getBillByInfo(String phone,String amt,String date){
		String sql = "select * from wt_pep_billpay where pepdate = "+date+" and tranamt = "+amt+" and phone = "+phone+" order by pepdate desc,peptime desc ";
		System.out.println("sql: "+sql);
		Collection result=new java.util.ArrayList();
		try {
			con = myOracle.getDataSource().getConnection();
			ps = con.prepareStatement(sql);
			rset=ps.executeQuery();
			while(rset.next()){
				String datenew = "";
				String bankcard ="";
				String phonenew = "";
				String paycde ="";
				String tranamt ="";
				String time = "";
				String paycard = "";
				String paydate ="";
				if(rset.getString("pay_card")!=null){
					paycard = rset.getString("pay_card");
				}
				if(rset.getString("pay_date")!=null){
					paydate = rset.getString("pay_date");
				}
				if(rset.getString("pepdate")!=null){
					datenew = rset.getString("pepdate");
				}
				if(rset.getString("bank_card")!=null){
					bankcard = rset.getString("bank_card");
				}
				if(rset.getString("phone")!=null){
					phonenew = rset.getString("phone");
				}
				if(rset.getString("bill_paycode")!=null){
					paycde = rset.getString("bill_paycode");
				}
				if(rset.getString("tranamt")!=null){
					tranamt = getTranamt(rset.getString("tranamt"));
				}
				if(rset.getString("peptime")!=null){
					time = rset.getString("peptime");
				}
				System.out.println("tranamt: "+tranamt);
				result.add(new wt_billpay(datenew,time,paycard,paydate,phonenew,tranamt,bankcard,paycde));
			}
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
	public void updateLog(String paycard,String paycde,String phone,String amt,String date,String time){
		String sql="";
		if(paycard!=null&&!paycard.equals("")){
		sql = "update wt_pep_billpay set bill_paycode = '"+paycde +"' ,pay_card = "+paycard +" where pepdate = "+date+" and tranamt = "+amt+" and phone = "+phone+" and peptime = "+time;
		}else{
		sql = "update wt_pep_billpay set bill_paycode = '"+paycde +" where pepdate = "+date+" and tranamt = "+amt+" and phone = "+phone+" and peptime = "+time;
		}
		try {
			System.out.println("sql "+sql);
			con = myOracle.getDataSource().getConnection();
			ps = con.prepareStatement(sql);
			int i= ps.executeUpdate();
			System.out.println("ddddd  "+i);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public String getTranamt(String str) {
		String tmp = "";
		char c[] = str.toCharArray();
		if (str.length() != 1) {
			for (int i = 0; i < c.length; i++) {
				// System.out.println(String.valueOf(c[i]).equals("0"));
				if (!String.valueOf(c[i + 1]).equals("0")) {
//					System.out.println("i " + i);
					tmp = str.substring(i + 1, str.length());
//					System.out.println(tmp);
					break;
				}
			}
		} else {
			tmp = str;
		}
		StringBuffer sb = new StringBuffer();
		sb.append(tmp);
		if (sb.length() > 2) {
			sb.insert(sb.length() - 2, ".");
		}
		if (sb.length() == 2) {
			sb.insert(0, "0.");
		}
		if (sb.length() == 1) {
			sb.insert(0, "0.0");
		}
//		System.out.println(sb.toString());
		return sb.toString();
	}
}
